package com.winit.openapi.sms;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.BpartnerService;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.sms.common.SmsUtils;
import com.winit.sms.spi.wallet.CheckAccountCommand;
import com.winit.sms.spi.wallet.LockedAccountCommand;
import com.winit.sms.spi.wallet.WalletService;
import com.winit.sms.spi.wallet.creditaccount.command.ModifyTransferAmountCommand;
import com.winit.sms.spi.wallet.creditaccount.command.QueryCreditAccountByProducLineCommand;
import com.winit.sms.spi.wallet.creditaccount.vo.CreditAccountVo;
import com.winit.ups.spi.systemrule.ChargeService;
import com.winit.ups.spi.systemrule.command.ChargeCommand;
import com.winit.ups.spi.systemrule.vo.Charge;

/**
 * 
 * 钱包消费服务
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * xiangui.fang 	1.0  		2016年2月22日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/sms/walletService")
public class WalletServiceController extends BaseController {

    private static Logger   logger = LoggerFactory.getLogger(CashAccountController.class);
    @Resource
    private WalletService   walletService;

    @Resource
    private ChargeService   chargeService;

    @Resource
    private BpartnerService bpartnerService;

    /**
     * 根据产品线查询对应的钱包帐户是否存在
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/queryCreditAcctByProducLine")
    public String queryCreditAcctByProducLine(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	QueryCreditAccountByProducLineCommand command = SmsUtils.toJavaObject(requestMsg.getData(), QueryCreditAccountByProducLineCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
        	CreditAccountVo vo = walletService.queryCreditAcctByCustomidAndProducLine(command);
        	if (vo == null || vo.getCustomerCode() == null) {
        		logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>没有对应的" + command.getProductLine() + "帐户");
        		responseMsg.setCode("1");
        	}
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }
    
    /**
     * 
     * 现金账户，信用账户验证
     * @return
     */
    @RequestMapping("/modifyCheckAcct")
    public String modifyCheckAcct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CheckAccountCommand command = SmsUtils.toJavaObject(requestMsg.getData(), CheckAccountCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
            walletService.checkBalance(command);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }

    /**
     * 冻结现金账户
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/modifyLockedCashAcct")
    public String modifyLockedCashAcct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        LockedAccountCommand command = SmsUtils.toJavaObject(requestMsg.getData(), LockedAccountCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
            walletService.lock(command);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }

    /**
     * 解冻现金账户
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/modifyUnlockedCashAcct")
    public String modifyUnlockedCashAcct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        LockedAccountCommand command = SmsUtils.toJavaObject(requestMsg.getData(), LockedAccountCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
            walletService.unlock(command);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }

    /**
     * 对现金账户扣款
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("modifyWithholdCashAcct")
    public String modifyWithholdCashAcct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        LockedAccountCommand command = SmsUtils.toJavaObject(requestMsg.getData(), LockedAccountCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
            walletService.consume(command);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }

    /**
     * 根据名称查询费用
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("chargeList")
    public String queryTransationName(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        ChargeCommand chargeCommand = new ChargeCommand();
		CommandContext ctx = CommandContext.getContext();
		chargeCommand.setCtx(ctx);
        List<Charge> chargList = chargeService.queryCharge(chargeCommand);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("chargList", chargList);
        responseMsg.setData(paramsMap);
        return SUCCESS;
    }

    /**
     * 查询用户的总余额
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("getTotalBalance")
    public String getTotalBalance(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CheckAccountCommand command = new CheckAccountCommand();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        BigDecimal totalBalance = walletService.getTotalBalance(command);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("totalBalance", totalBalance);
        responseMsg.setData(paramsMap);
        return SUCCESS;
    }
    
    /**
     * 钱包帐户之间转帐功能
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("modifyTransferAmount")
    public String modifyTransferAmount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	ModifyTransferAmountCommand command = SmsUtils.toJavaObject(requestMsg.getData(), ModifyTransferAmountCommand.class);
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner == null) {
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>bpartner对象为空");
            return ERROR;
        }
        command.setCustomerCode(bpartner.getValue());
        try {
            walletService.modifyTransferAmount(command);
        } catch (Exception e) {
        	responseMsg.setCode(e.getMessage());
        	responseMsg.setMsg(e.getLocalizedMessage());
        	return ERROR;
        }
        return SUCCESS;
    }
}
